package es.buffygilmore.gastosapp;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class EventoListado extends ListActivity {

	private static final int ACTIVITY_CREATE_EVENTO = 0;
	private static final int ACTIVITY_EDIT_EVENTO = 1;
	private static final int ACTIVITY_ACCESS_GASTOS = 2;

	private GastosAppDbAdapter mDbHelper;

	private static final int INSERT_ID = Menu.FIRST;
	private static final int MODIFY_ID = Menu.FIRST + 1;
	private static final int DELETE_ID = Menu.FIRST + 2;

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.listado_eventos);
		this.mDbHelper = new GastosAppDbAdapter(this);
		this.mDbHelper.open();
		this.fillEventosData();
		this.registerForContextMenu(this.getListView());
	}

	private void fillEventosData() {
		// Get all of the rows from the database and create the item list
		final Cursor eventosCursor = this.mDbHelper.obtenerTodosEventos();
		this.startManagingCursor(eventosCursor);

		// Create an array to specify the fields we want to display in the list (only TITLE)
		final String[] from = new String[] { GastosAppDbAdapter.KEY_EVENTO_TITULO };

		// and an array of the fields we want to bind those fields to (in this case just text1)
		final int[] to = new int[] { R.id.eventoTitulo };

		// Now create a simple cursor adapter and set it to display
		final SimpleCursorAdapter eventos = new SimpleCursorAdapter(this, R.layout.fila_evento,
				eventosCursor, from, to);
		this.setListAdapter(eventos);
	}

	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, R.string.menu_insertar_evento);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
		switch (item.getItemId()) {
		case INSERT_ID:
			this.crearColeccion();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public void onCreateContextMenu(final ContextMenu menu, final View v,
			final ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_ID, 0, R.string.menu_eliminar_evento);
		menu.add(0, MODIFY_ID, 0, R.string.menu_modificar_evento);
	}

	@Override
	public boolean onContextItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_ID:
			final AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			this.mDbHelper.eliminarEvento(info.id);
			this.fillEventosData();
			return true;
		case MODIFY_ID:
			// TODO
			final AdapterContextMenuInfo infoModif = (AdapterContextMenuInfo) item.getMenuInfo();
			final Intent i = new Intent(this, EventoDetalle.class);
			i.putExtra(GastosAppDbAdapter.KEY_EVENTO_ROWID, infoModif.id);
			this.startActivityForResult(i, ACTIVITY_EDIT_EVENTO);

			return true;
		}

		return super.onContextItemSelected(item);
	}

	private void crearColeccion() {
		final Intent i = new Intent(this, EventoDetalle.class);
		this.startActivityForResult(i, ACTIVITY_CREATE_EVENTO);
	}

	@Override
	protected void onListItemClick(final ListView l, final View v, final int position, final long id) {
		super.onListItemClick(l, v, position, id);
		// final Intent i = new Intent(this, GastoListado.class);
		// i.putExtra(GastosAppDbAdapter.KEY_EVENTO_ROWID, id);
		// // Se recupera la coleccion y se pasa
		// final Cursor eventoSeleccionado = this.mDbHelper.obtenerEvento(id);
		// i.putExtra(GastosAppDbAdapter.KEY_EVENTO_TITULO, eventoSeleccionado
		// .getString(eventoSeleccionado
		// .getColumnIndexOrThrow(GastosAppDbAdapter.KEY_EVENTO_TITULO)));
		// this.startActivityForResult(i, ACTIVITY_ACCESS_GASTOS);
	}

	@Override
	protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		this.fillEventosData();
	}
}
